package com.neteast.androidclient.newscenter.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.neteast.androidclient.newscenter.db.InfoDao;
import com.neteast.androidclient.newscenter.domain.Information;

public class ImageUtil{
	
	public static File getSaveDir(Context context) {
		File saveDir=null;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			saveDir=new File(Environment.getExternalStorageDirectory(), "newscenter/picture");
			saveDir.mkdirs();
		}else {
			saveDir=context.getDir("newscenter/picture", Context.MODE_PRIVATE);
		}
		return saveDir;
	}
	
	public static File makeSaveFile(Information information,Context context) {
		File saveDir = getSaveDir(context);
		String picUrl = information.picUrl;
		if (!TextUtils.isEmpty(picUrl)) {
			String suffix = picUrl.substring(picUrl.lastIndexOf("."));
			String fileName = information.infoId + suffix;
			File file = new File(saveDir, fileName);
			file.getParentFile().mkdirs();
			return file;
		}
		return null;
	}
	
	public static Bitmap getImageFile(Information information,Context context) {
		File file = makeSaveFile(information, context);
		Log.i("test", file.getAbsolutePath());
		if (file!=null&&file.exists()&&file.isFile()) {
			String imagePath = file.getAbsolutePath();
			Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
			return ThumbnailUtils.extractThumbnail(bitmap, 120, 120);
		}
		return null;
	}
	
	public static String downLoadImage(String path, File imageFile)
			throws Exception {
		
		if (imageFile.exists()&&imageFile.length()>0) {
			return imageFile.getAbsolutePath();
		} else {
			imageFile.createNewFile();
		}
		URL url = new URL(path);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setConnectTimeout(5000);
		connection.setRequestMethod("GET");
		if (connection.getResponseCode() == 200) {
			saveImage(connection.getInputStream(), imageFile);
			return imageFile.getAbsolutePath();
		}
		return null;
	}

	private static void saveImage(InputStream inputStream, File imageFile)
			throws Exception {
		FileOutputStream outputStream = new FileOutputStream(imageFile);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inputStream.read(buffer)) > 0) {
			outputStream.write(buffer, 0, len);
		}
		inputStream.close();
		outputStream.close();
	}
	
	public static void deleteInfo(Information info,Context context) {
		int userId = LoginUtil.getUserId(context);
		InfoDao dao=new InfoDao(context);
		dao.open();
		dao.deleteInfo(info, userId);
		dao.close();
		File saveFile = makeSaveFile(info, context);
		if (saveFile!=null&&saveFile.exists()&&saveFile.isFile()) {
			saveFile.delete();
		}
	}
}
